package b3.model
{
	import b3.config.*;
	import b3.net.*;
	
	public class StateModel extends Model
	{
		public var ownerId:int;
		
		public var stateList:Array;
		
		public function StateModel(ownerID:int)
		{
			super();
			//stateModel 应该记录下每一个状态对应的效果，并且记录下他们的截止时间和使用次数
			stateList = new Array();
			this.ownerId = ownerID;
		}
		
		/**
		 * 初始化的时候载入
		 */ 
		public function loadState(force:Boolean):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.LOAD_STATES, {ownerId:ownerId});
			serverRequest.setHandleFunc(handleLoadStates);
			serverRequest.post(force);
		}
		/**
		 * 处理载入的返回函数
		 */ 
		public function handleLoadStates(data:Object):void
		{
			var stateForBuild:Object = data['stateForBuild'];
			stateList = new Array();
			for(var s:String in stateForBuild)
			{
				var stateInfo:StateInfo = new StateInfo();
				stateInfo.loadbyObject(stateForBuild[s]);
				stateList.push(stateInfo); 
			}
			dispatchEvent(new ModelEvent(ModelEvent.REFRESH));
		}
		
		public function hasState(state:int):Boolean
		{
			for (var i:int = 0; i < stateList.length; i++)
			{
				var stateInfo:StateInfo = stateList[i];
				if (stateInfo.state == state)
					return true;
			}
			return false;
		}
	}
	
	
}